package com.sys.userrole.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.role.entity.SysRole;
import com.sys.role.service.SysRoleService;
import com.sys.user.entity.SysUser;
import com.sys.user.service.SysUserService;
import com.sys.userrole.entity.SysUserRole;
import com.sys.userrole.mapper.SysUserRoleMapper;
import com.sys.userrole.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author djianfeng
 * @since 2018-07-24
 */
@Transactional
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    @Autowired
    SysUserService userService;
    @Autowired
    SysRoleService roleService;

    /**
     * 获取用户的角色
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> findRolesByUserId(Long userId) {
        Map map = new HashMap();
        SysUser user = this.userService.selectById(userId);
        if (!user.getUserType().equals("0")) {
            map.put("user_id", userId);
            List<SysUserRole> list = (List)this.selectByMap(map);
            Set<String> set = new HashSet();
            for (SysUserRole ur : list) {
                set.add(ur.getRoleId().toString());
            }
            return set;
        }
        //超管
        List<SysRole> list = (List)roleService.selectByMap(map);
        Set<String> set = new HashSet();
        for (SysRole ur : list) {
            set.add(ur.getId().toString());
        }
        return set;
    }
}
